package com.hbnu.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.mapper.DeptMapper;
import com.hbnu.system.model.entity.*;
import com.hbnu.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {
    @Autowired
    private IPaperService iPaperService;

    @Autowired
    private IPatentService iPatentService;

    @Autowired
    private IWorkService iWorkService;

    @Autowired
    private IAwardService iAwardService;

    @Autowired
    private IArtService iArtService;

    @Autowired
    private IReportService iReportService;

    @Autowired
    private IIdentifyService iIdentifyService;

    @Autowired
    private IOpusService iOpusService;

    @Override
    public boolean addDept(Dept dept) {
        // 获取当前数据库中 deptName_id 的最大值
        Integer maxDeptNameId = baseMapper.selectMaxDeptNameId();
        // 设置 deptNameId 为最大值加一
        dept.setDeptNameId(maxDeptNameId == null ? 1 : maxDeptNameId + 1);
        return baseMapper.insert(dept) == 1;
    }

    @Override
    public List<Dept> getDeptList() {
        List<Dept> depts = baseMapper.selectList(Wrappers.lambdaQuery(Dept.class).orderByAsc(Dept::getDeptNameId));
        return depts;
    }

    @Override
    public boolean updateDept(Dept dept) {
        String oldDept = baseMapper.selectOne(Wrappers.lambdaQuery(Dept.class)
                .eq(Dept::getDeptID,dept.getDeptID())).getDeptName();

        List<Paper> paperList = iPaperService.list(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getDept, oldDept));
        for (Paper paper : paperList) {
            paper.setDept(dept.getDeptName());
            iPaperService.updateById(paper);
        }

        List<Patent> patentList = iPatentService.list(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getDept, oldDept));
        for (Patent patent : patentList) {
            patent.setDept(dept.getDeptName());
            iPatentService.updateById(patent);
        }

        List<Award> awardList = iAwardService.list(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getDept, oldDept));
        for (Award award : awardList) {
            award.setDept(dept.getDeptName());
            iAwardService.updateById(award);
        }

        List<Work> workList = iWorkService.list(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getDept, oldDept));
        for (Work work : workList) {
            work.setDept(dept.getDeptName());
            iWorkService.updateById(work);
        }

        List<Art> artList = iArtService.list(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getDept, oldDept));
        for (Art art : artList) {
            art.setDept(dept.getDeptName());
            iArtService.updateById(art);
        }

        List<Identify> identifyList = iIdentifyService.list(Wrappers.lambdaQuery(Identify.class)
                .eq(Identify::getDept, oldDept));
        for (Identify identify : identifyList) {
            identify.setDept(dept.getDeptName());
            iIdentifyService.updateById(identify);
        }

        List<Report> reportList = iReportService.list(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getDept, oldDept));
        for (Report report : reportList) {
            report.setDept(dept.getDeptName());
            iReportService.updateById(report);
        }

        List<Opus> opusList = iOpusService.list(Wrappers.lambdaQuery(Opus.class)
                .eq(Opus::getDept, oldDept));
        for (Opus opus : opusList) {
            opus.setDept(dept.getDeptName());
            iOpusService.updateById(opus);
        }

        return baseMapper.updateById(dept) == 1;
    }

    @Override
    public boolean deleteDept(String deptID) {
        return baseMapper.deleteById(deptID) == 1;
    }

    @Override
    public List<Dept> getDeptByName(String deptName) {

        LambdaQueryWrapper<Dept> queryWrapper = Wrappers.lambdaQuery(Dept.class);

        // 使用 Java 标准库方法检查 deptName 是否为空或空白
        if (deptName != null && !deptName.trim().isEmpty()) {
            queryWrapper.like(Dept::getDeptName, deptName);
        }

        return baseMapper.selectList(queryWrapper);
    }
}
